﻿using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using XH.SmartParking.IService;
using XH.SmartParking.Base;
using XH.SmartParking.Entities;
using XH.SmartParking.Models;
using XH.SmartParking.Service;

namespace XH.SmartParking.ViewModels.Pages
{
    public class DeviceViewModel : ViewModelBase
    {
        public ObservableCollection<DeviceModel> Devices { get; set; } =
            new ObservableCollection<DeviceModel>();

        IDeviceService _deviceService;
        IDialogService _dialogService;
        public DeviceViewModel(
            IRegionManager regionManager,
            IDeviceService deviceService,
            IDialogService dialogService)
            : base(regionManager)
        {
            PageTitle = "设备信息维护";

            _deviceService = deviceService;
            _dialogService = dialogService;

            Refresh();
        }

        public override void Refresh()
        {
            Devices.Clear();

            var ds = _deviceService.Query<DeviceInfo>(

                d =>
                string.IsNullOrEmpty(SearchKey) ||
                d.DeviceName.Contains(SearchKey)

                );

            int index = 1;
            foreach (var item in ds)
            {
                Devices.Add(new DeviceModel
                {
                    Index = index++,
                    DeviceId = item.DeviceId,
                    DeviceName = item.DeviceName,
                    AddrIp = item.AddrIp,
                    AddrPort = item.AddrPort,
                    AddrCom = item.AddrCom,
                    UserName = item.UserName,
                    Password = item.Password,
                    CallbackUrl1 = item.CallbackUrl1,
                    CallbackUrl2 = item.CallbackUrl2,
                    CallbackUrl3 = item.CallbackUrl3
                });
            }
        }

        public override void DoModify(object model)
        {
            DialogParameters ps = new DialogParameters();
            ps.Add("model", model);
            _dialogService.ShowDialog("ModifyDeviceView", ps, result =>
            {
                // 判断子窗口的返回状态，如果OK，刷新当前页面，否则不管
                if (result.Result == ButtonResult.OK)
                {
                    Refresh();
                }
            });
        }

        public override void DoDelete(object model)
        {
            try
            {
                if (MessageBox.Show("是否确定删除此项？", "提示", MessageBoxButton.YesNo) ==
                    MessageBoxResult.Yes)
                {
                    // 物理删除
                    _deviceService.Delete<DeviceInfo>((model as DeviceModel).DeviceId);

                    MessageBox.Show("删除完成！", "提示");

                    Devices.Remove(model as DeviceModel);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }
    }
}
